#include "bsp.h"

#if HardwareType == Common_Hardware_Type

UART_HandleTypeDef UART1Handle;
UART_HandleTypeDef UART2Handle;
UART_HandleTypeDef UART3Handle;
UART_HandleTypeDef UART4Handle;
UART_HandleTypeDef UART5Handle;
UART_HandleTypeDef UART6Handle;

DMA_HandleTypeDef hdma_u2tx;

uint8_t uart1_rx_data = 0;
uint8_t uart2_rx_data = 0;
//uint8_t uart3_rx_data[2][POWER_DATA_LENGTH];
uint8_t uart3_rx_data[POWER_DATA_LENGTH];
uint8_t uart4_rx_data[2][RC_FRAME_LENGTH];
uint8_t uart5_rx_data[2][55u];
uint8_t uart6_rx_data = 0;

__weak void BSP_UART1_RX_Callback(uint8_t rxdata)
{
    return;
}

__weak void BSP_UART2_RX_Callback(uint8_t rxdata)
{
    return;
}

__weak void BSP_UART3_RX_Callback(uint8_t rxdata[])
{
    return;
}

__weak void BSP_UART4_RX_Callback(uint8_t rxdata[])
{
    return;
}

__weak void BSP_UART5_RX_Callback(uint8_t rxdata[])
{
    return;
}

__weak void BSP_UART6_RX_Callback(uint8_t rxdata)
{
    return;
}

uint8_t  length = 0;
uint8_t  uart5RevDataLength = 0;
__weak void MY_UART_IRQHandler(UART_HandleTypeDef* huart)
{
    if(huart->Instance == UART4)
    {
               uint8_t uart4RevDataLength = 0;           //此次接收完成的数据长度
        static uint16_t FailNumCount0 = 0, FailNumCount1 = 0;
        if(__HAL_UART_GET_IT_SOURCE(&UART4Handle, UART_IT_IDLE) != RESET)
        {
            __HAL_UART_CLEAR_IDLEFLAG(&UART4Handle);
            uart4RevDataLength = RC_FRAME_LENGTH - __HAL_DMA_GET_COUNTER(UART4Handle.hdmarx);
            
			if(UART4Handle.hdmarx->Instance->CR & DMA_SxCR_CT)   //当前正在访问缓冲区1
            {
                if(uart4RevDataLength > 0 && uart4RevDataLength < RC_FRAME_LENGTH)        //接收到不完整的帧数据
                {
                    __HAL_DMA_DISABLE(UART4Handle.hdmarx);
                    UART4Handle.hdmarx->Instance->NDTR = (uint16_t)RC_FRAME_LENGTH;
                    UART4Handle.hdmarx->Instance->CR |= DMA_SxCR_CT;            //传输错误，修改缓冲区，丢弃此帧数据
                    __HAL_DMA_ENABLE(UART4Handle.hdmarx);
                    FailNumCount1++;
                }
                else
                {
                    BSP_UART4_RX_Callback(uart4_rx_data[1]);
                }
            }
            else//当前正在访问缓冲区0
            {
                if(uart4RevDataLength > 0 && uart4RevDataLength < RC_FRAME_LENGTH)
                {
                    __HAL_DMA_DISABLE(UART4Handle.hdmarx);
                    UART4Handle.hdmarx->Instance->NDTR = (uint16_t)RC_FRAME_LENGTH;
                    UART4Handle.hdmarx->Instance->CR &= ~DMA_SxCR_CT;
                    __HAL_DMA_ENABLE(UART4Handle.hdmarx);
                    FailNumCount0++;
                }
                else
                {
                    BSP_UART4_RX_Callback(uart4_rx_data[0]);
                }
            }
        }
    }
    else if(huart->Instance == UART5)
    {
        
        if(__HAL_UART_GET_IT_SOURCE(&UART5Handle, UART_IT_IDLE) != RESET)
        {
            __HAL_UART_CLEAR_IDLEFLAG(&UART5Handle);
            uart5RevDataLength = GYRO_DATA_MAX - __HAL_DMA_GET_COUNTER(UART5Handle.hdmarx);
            length = uart5RevDataLength;
            if(UART5Handle.hdmarx->Instance->CR & DMA_SxCR_CT)//当前正在访问缓冲区1
            {
                if(((uart5RevDataLength > 0) && (uart5RevDataLength < GYRO_ANGLE_DATA_LEN))		//接收数据错位，丢弃此帧数据
				||((uart5RevDataLength > GYRO_ANGLE_DATA_LEN) && (uart5RevDataLength < GYRO_ANGV_DATA_LEN))
                ||((uart5RevDataLength > GYRO_ANGV_DATA_LEN) && (uart5RevDataLength < GYRO_DATA_MAX))
                )
                {
                    __HAL_DMA_DISABLE(UART5Handle.hdmarx);
                    UART5Handle.hdmarx->Instance->NDTR = (uint16_t)GYRO_DATA_MAX;
                    UART5Handle.hdmarx->Instance->CR  |= DMA_SxCR_CT;
                    __HAL_DMA_ENABLE(UART5Handle.hdmarx);
                }
                else
                {
					__HAL_DMA_DISABLE(UART5Handle.hdmarx);			//接收数据期间关闭DMA数据流，否则接收会出错
                   if(uart5RevDataLength == GYRO_ANGLE_DATA_LEN)	//接收数据长度为25 < 49,此时需要软件修改缓冲区并且重置数据长度
                   {
                        UART5Handle.hdmarx->Instance->NDTR = (uint16_t)GYRO_DATA_MAX;
                        UART5Handle.hdmarx->Instance->CR  |= DMA_SxCR_CT;
                   }
                   else if(uart5RevDataLength == GYRO_ANGV_DATA_LEN)
                   {
                     UART5Handle.hdmarx->Instance->NDTR = (uint16_t)GYRO_DATA_MAX;
                     UART5Handle.hdmarx->Instance->CR  |= DMA_SxCR_CT;
                   }
                    
                    BSP_UART5_RX_Callback(uart5_rx_data[1]);
					__HAL_DMA_ENABLE(UART5Handle.hdmarx);
                }
            }
            else
            {
                if(((uart5RevDataLength > 0) && (uart5RevDataLength < GYRO_ANGLE_DATA_LEN))
                ||((uart5RevDataLength > GYRO_ANGLE_DATA_LEN) && (uart5RevDataLength < GYRO_ANGV_DATA_LEN))
                ||((uart5RevDataLength > GYRO_ANGV_DATA_LEN) && (uart5RevDataLength < GYRO_DATA_MAX))
                )
                {
                    __HAL_DMA_DISABLE(UART5Handle.hdmarx);
                    UART5Handle.hdmarx->Instance->NDTR = (uint16_t)GYRO_DATA_MAX;
                    UART5Handle.hdmarx->Instance->CR  &= ~DMA_SxCR_CT;
                    __HAL_DMA_ENABLE(UART5Handle.hdmarx);
                }
                else
                {
					__HAL_DMA_DISABLE(UART5Handle.hdmarx);
                   if(uart5RevDataLength == GYRO_ANGLE_DATA_LEN)
                   {
                       UART5Handle.hdmarx->Instance->NDTR = (uint16_t)GYRO_DATA_MAX;
                       UART5Handle.hdmarx->Instance->CR  &= ~DMA_SxCR_CT;
                   }
                   else if(uart5RevDataLength == GYRO_ANGV_DATA_LEN)
                   {
                     UART5Handle.hdmarx->Instance->NDTR = (uint16_t)GYRO_DATA_MAX;
                     UART5Handle.hdmarx->Instance->CR  &= ~DMA_SxCR_CT;
                   }

                    BSP_UART5_RX_Callback(uart5_rx_data[0]);
					__HAL_DMA_ENABLE(UART5Handle.hdmarx);
                }
            }
        }
    }
}


void HAL_UART_ErrorCallback(UART_HandleTypeDef *huart)
{
	if(huart->Instance == UART3Handle.Instance)
	{
		__HAL_UART_CLEAR_OREFLAG(&UART3Handle);
		HAL_UART_Receive_IT(&UART3Handle, uart3_rx_data, 9);
	}
	else if(huart->Instance == UART5Handle.Instance)
	{
		__HAL_UART_CLEAR_OREFLAG(&UART5Handle);
		//HAL_UART_Receive_IT(&UART5Handle,&uart5_rx_data,1);
	}
	else if(huart->Instance == UART4Handle.Instance)
	{
		__HAL_UART_CLEAR_OREFLAG(&UART4Handle);
		//HAL_UART_Receive_IT(&UART4Handle,uart4_rx_data,18);
	}
	else if(huart->Instance == UART2Handle.Instance)
	{
		__HAL_UART_CLEAR_OREFLAG(&UART2Handle);
		HAL_UART_Receive_IT(&UART2Handle,&uart2_rx_data,1);
	}
}

void UART1_Init(uint32_t boundrate)
{
    UART1Handle.Instance            = USART1;
    UART1Handle.Init.BaudRate       = boundrate;
    UART1Handle.Init.WordLength     = UART_WORDLENGTH_8B;
    UART1Handle.Init.StopBits       = UART_STOPBITS_1;
    UART1Handle.Init.Parity         = UART_PARITY_NONE;
    UART1Handle.Init.Mode           = UART_MODE_TX_RX;
    UART1Handle.Init.HwFlowCtl      = UART_HWCONTROL_NONE;
    UART1Handle.Init.OverSampling   = UART_OVERSAMPLING_16;
    if (HAL_UART_Init(&UART1Handle) != HAL_OK)
    {
        _Error_Handler(__FILE__, __LINE__);
    }

     if(HAL_UART_Receive_IT(&UART1Handle, &uart1_rx_data, 1) != HAL_OK)
     {
         _Error_Handler(__FILE__, __LINE__);
     }    
}

void UART2_Init(uint32_t boundrate)
{

    UART2Handle.Instance            = USART2;
    UART2Handle.Init.BaudRate       = boundrate;
    UART2Handle.Init.WordLength     = UART_WORDLENGTH_8B;
    UART2Handle.Init.StopBits       = UART_STOPBITS_1;
    UART2Handle.Init.Parity         = UART_PARITY_NONE;
    UART2Handle.Init.Mode           = UART_MODE_TX_RX;
    UART2Handle.Init.HwFlowCtl      = UART_HWCONTROL_NONE;
    UART2Handle.Init.OverSampling   = UART_OVERSAMPLING_16;
    if (HAL_UART_Init(&UART2Handle) != HAL_OK)
    {
        _Error_Handler(__FILE__, __LINE__);
    }

    if(HAL_UART_Receive_IT(&UART2Handle, &uart2_rx_data, 1) != HAL_OK)
    {
        _Error_Handler(__FILE__, __LINE__);
    }
}

void UART3_Init(uint32_t boundrate)
{
    UART3Handle.Instance            = USART3;
    UART3Handle.Init.BaudRate       = boundrate;
    UART3Handle.Init.WordLength     = UART_WORDLENGTH_8B;
    UART3Handle.Init.StopBits       = UART_STOPBITS_1;
    UART3Handle.Init.Parity         = UART_PARITY_NONE;
    UART3Handle.Init.Mode           = UART_MODE_TX_RX;
    UART3Handle.Init.HwFlowCtl      = UART_HWCONTROL_NONE;
    UART3Handle.Init.OverSampling   = UART_OVERSAMPLING_16;
    if (HAL_UART_Init(&UART3Handle) != HAL_OK)
    {
        _Error_Handler(__FILE__, __LINE__);
    }

    //__HAL_DMA_ENABLE(UART3Handle.hdmatx);
    //__HAL_DMA_ENABLE(UART3Handle.hdmarx);
    //__HAL_UART_ENABLE_IT(&UART3Handle, USART_CR1_RXNEIE);
    if(HAL_UART_Receive_IT(&UART3Handle, uart3_rx_data, 9) != HAL_OK)
    {
        _Error_Handler(__FILE__, __LINE__);
    }
}

void UART4_Init(uint32_t boundrate)
{
	
    UART4Handle.Instance            = UART4;
    UART4Handle.Init.BaudRate       = boundrate;
    UART4Handle.Init.WordLength     = UART_WORDLENGTH_8B;
    UART4Handle.Init.StopBits       = UART_STOPBITS_1;
    UART4Handle.Init.Parity         = UART_PARITY_NONE;
    UART4Handle.Init.Mode           = UART_MODE_RX;
    UART4Handle.Init.HwFlowCtl      = UART_HWCONTROL_NONE;
    UART4Handle.Init.OverSampling   = UART_OVERSAMPLING_16;
    if (HAL_UART_Init(&UART4Handle) != HAL_OK)
    {
        _Error_Handler(__FILE__, __LINE__);
    }
    __HAL_DMA_ENABLE(UART4Handle.hdmarx);               //使能DMA数据流
    __HAL_UART_ENABLE_IT(&UART4Handle, UART_IT_IDLE);   //开启串口空闲中断
}

void UART5_Init(uint32_t boundrate)
{
    UART5Handle.Instance            = UART5;
    UART5Handle.Init.BaudRate       = boundrate; 
    UART5Handle.Init.WordLength     = UART_WORDLENGTH_8B;
    UART5Handle.Init.StopBits       = UART_STOPBITS_1;
    UART5Handle.Init.Parity         = UART_PARITY_NONE;
    UART5Handle.Init.Mode           = UART_MODE_TX_RX;
    UART5Handle.Init.HwFlowCtl      = UART_HWCONTROL_NONE;
    UART5Handle.Init.OverSampling   = UART_OVERSAMPLING_16;
    if (HAL_UART_Init(&UART5Handle) != HAL_OK)
    {
        _Error_Handler(__FILE__, __LINE__);
    }
    __HAL_DMA_ENABLE(UART5Handle.hdmarx);				//使能DMA数据流
    __HAL_UART_ENABLE_IT(&UART5Handle, UART_IT_IDLE);	//开启串口空闲中断
       
}

void UART6_Init(uint32_t boundrate)
{

    UART6Handle.Instance            = USART6;
    UART6Handle.Init.BaudRate       = boundrate;
    UART6Handle.Init.WordLength     = UART_WORDLENGTH_8B;
    UART6Handle.Init.StopBits       = UART_STOPBITS_1;
    UART6Handle.Init.Parity         = UART_PARITY_NONE;
    UART6Handle.Init.Mode           = UART_MODE_TX_RX;
    UART6Handle.Init.HwFlowCtl      = UART_HWCONTROL_NONE;
    UART6Handle.Init.OverSampling   = UART_OVERSAMPLING_16;
    if (HAL_UART_Init(&UART6Handle) != HAL_OK)
    {
        _Error_Handler(__FILE__, __LINE__);
    }

    if(HAL_UART_Receive_IT(&UART6Handle, &uart6_rx_data, 1) != HAL_OK)
    {
        _Error_Handler(__FILE__, __LINE__);
    }    
}

void HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart)
{
    if(huart->Instance == USART1)
    {
        BSP_UART1_RX_Callback(uart1_rx_data);

        if(HAL_UART_Receive_IT(&UART1Handle, &uart1_rx_data, 1) != HAL_OK)
        {
            _Error_Handler(__FILE__, __LINE__);
        }
    } 
    else if(huart->Instance == USART2)
    {
        BSP_UART2_RX_Callback(uart2_rx_data);

        if(HAL_UART_Receive_IT(&UART2Handle, &uart2_rx_data, 1) != HAL_OK)
        {
            _Error_Handler(__FILE__, __LINE__);
        } 
    }
    else if(huart->Instance == USART3)
    {
        BSP_UART3_RX_Callback(uart3_rx_data);

        if(HAL_UART_Receive_IT(&UART3Handle, uart3_rx_data, 8) != HAL_OK)
        {
            _Error_Handler(__FILE__, __LINE__);
        } 
    }        
    else if(huart->Instance == UART4)
    {

    }    
    else if(huart->Instance == UART5)
    {
         
    }  
    else if(huart->Instance == USART6)
    {
        BSP_UART6_RX_Callback(uart6_rx_data);

        if(HAL_UART_Receive_IT(&UART6Handle, &uart6_rx_data, 1) != HAL_OK)
        {
            _Error_Handler(__FILE__, __LINE__);
        } 
    }         
}

extern uint8_t clientTxData[28];
void HAL_UART_MspInit(UART_HandleTypeDef *huart)
{
    GPIO_InitTypeDef  GPIO_InitStruct = {0};

    static DMA_HandleTypeDef hdma_u1tx;
    static DMA_HandleTypeDef hdma_u1rx;

    static DMA_HandleTypeDef hdma_u3tx;
    static DMA_HandleTypeDef hdma_u3rx;
  
    static DMA_HandleTypeDef hdma_u4rx;

    static DMA_HandleTypeDef hdma_u5rx;
		
    GPIO_InitStruct.Mode      = GPIO_MODE_AF_PP;
    GPIO_InitStruct.Pull      = GPIO_PULLUP;
    GPIO_InitStruct.Speed     = GPIO_SPEED_FREQ_VERY_HIGH;

    if(huart->Instance == USART1)
    {
        __HAL_RCC_USART1_CLK_ENABLE();

        UART1_TX_PORT_CLK_ENABLE();
        UART1_RX_PORT_CLK_ENABLE();

        GPIO_InitStruct.Alternate = GPIO_AF7_USART1;
        GPIO_InitStruct.Pin       = UART1_TX_PIN;
        HAL_GPIO_Init(UART1_TX_PORT, &GPIO_InitStruct);
        
        GPIO_InitStruct.Alternate = GPIO_AF7_USART1;
        GPIO_InitStruct.Pin       = UART1_RX_PIN;
        HAL_GPIO_Init(UART1_RX_PORT, &GPIO_InitStruct);

        __HAL_RCC_DMA2_CLK_ENABLE();

        hdma_u1tx.Instance                 = DMA2_Stream7;
        hdma_u1tx.Init.Channel             = DMA_CHANNEL_4;
        hdma_u1tx.Init.Direction           = DMA_MEMORY_TO_PERIPH;
        hdma_u1tx.Init.PeriphInc           = DMA_PINC_DISABLE;
        hdma_u1tx.Init.MemInc              = DMA_MINC_ENABLE;
        hdma_u1tx.Init.PeriphDataAlignment = DMA_PDATAALIGN_BYTE;
        hdma_u1tx.Init.MemDataAlignment    = DMA_MDATAALIGN_BYTE;
        hdma_u1tx.Init.Mode                = DMA_NORMAL;
        hdma_u1tx.Init.Priority            = DMA_PRIORITY_LOW;
        hdma_u1tx.Init.FIFOMode            = DMA_FIFOMODE_DISABLE;
        hdma_u1tx.Init.FIFOThreshold       = DMA_FIFO_THRESHOLD_FULL;
        hdma_u1tx.Init.MemBurst            = DMA_MBURST_INC4;
        hdma_u1tx.Init.PeriphBurst         = DMA_PBURST_INC4;
        HAL_DMA_Init(&hdma_u1tx);
        __HAL_LINKDMA(huart, hdmatx, hdma_u1tx);


        hdma_u1rx.Instance                 = DMA2_Stream5;
        hdma_u1rx.Init.Channel             = DMA_CHANNEL_4;
        hdma_u1rx.Init.Direction           = DMA_PERIPH_TO_MEMORY;
        hdma_u1rx.Init.PeriphInc           = DMA_PINC_DISABLE;
        hdma_u1rx.Init.MemInc              = DMA_MINC_ENABLE;
        hdma_u1rx.Init.PeriphDataAlignment = DMA_PDATAALIGN_BYTE;
        hdma_u1rx.Init.MemDataAlignment    = DMA_MDATAALIGN_BYTE;
        hdma_u1rx.Init.Mode                = DMA_NORMAL;
        hdma_u1rx.Init.Priority            = DMA_PRIORITY_HIGH;
        hdma_u1rx.Init.FIFOMode            = DMA_FIFOMODE_DISABLE;         
        hdma_u1rx.Init.FIFOThreshold       = DMA_FIFO_THRESHOLD_FULL;
        hdma_u1rx.Init.MemBurst            = DMA_MBURST_INC4;
        hdma_u1rx.Init.PeriphBurst         = DMA_PBURST_INC4; 
        HAL_DMA_Init(&hdma_u1rx);
        __HAL_LINKDMA(huart, hdmarx, hdma_u1rx);

        HAL_NVIC_SetPriority(DMA2_Stream7_IRQn, 0, 1);
        HAL_NVIC_EnableIRQ(DMA2_Stream7_IRQn);

        HAL_NVIC_SetPriority(DMA2_Stream5_IRQn, 0, 0);   
        HAL_NVIC_EnableIRQ(DMA2_Stream5_IRQn);
        
        HAL_NVIC_SetPriority(USART1_IRQn, 0, 0);
        HAL_NVIC_EnableIRQ(USART1_IRQn);                
    }
    else if(huart->Instance == USART2)
    {
        __HAL_RCC_USART2_CLK_ENABLE();

        UART2_TX_PORT_CLK_ENABLE();
        UART2_RX_PORT_CLK_ENABLE();

        GPIO_InitStruct.Alternate = GPIO_AF7_USART2;
        GPIO_InitStruct.Pin       = UART2_TX_PIN;
        HAL_GPIO_Init(UART2_TX_PORT, &GPIO_InitStruct);
        
        GPIO_InitStruct.Alternate = GPIO_AF7_USART2;
        GPIO_InitStruct.Pin       = UART2_RX_PIN;
        HAL_GPIO_Init(UART2_RX_PORT, &GPIO_InitStruct);

        /* DMA controller clock enable */
        __HAL_RCC_DMA1_CLK_ENABLE();

		hdma_u2tx.Instance                 = DMA1_Stream6;
		hdma_u2tx.Init.Channel             = DMA_CHANNEL_4;
		hdma_u2tx.Init.Direction           = DMA_MEMORY_TO_PERIPH;
		hdma_u2tx.Init.PeriphInc           = DMA_PINC_DISABLE;
		hdma_u2tx.Init.MemInc              = DMA_MINC_ENABLE;
		hdma_u2tx.Init.PeriphDataAlignment = DMA_PDATAALIGN_BYTE;
		hdma_u2tx.Init.MemDataAlignment    = DMA_MDATAALIGN_BYTE;
		hdma_u2tx.Init.Mode                = DMA_NORMAL;
		hdma_u2tx.Init.Priority            = DMA_PRIORITY_LOW;
		hdma_u2tx.Init.FIFOMode            = DMA_FIFOMODE_DISABLE;
		
		HAL_DMA_Init(&hdma_u2tx);
        SET_BIT(UART2Handle.Instance->CR3, USART_CR3_DMAT);
        hdma_u2tx.Instance->PAR = (uint32_t)&USART2->DR;
        hdma_u2tx.Instance->M0AR = (uint32_t)clientTxData;

		__HAL_LINKDMA(huart,hdmatx,hdma_u2tx);
		
		HAL_NVIC_SetPriority(USART2_IRQn, 0, 0);
        HAL_NVIC_EnableIRQ(USART2_IRQn);    

    }  
    else if(huart->Instance == USART3)
    {
        __HAL_RCC_USART3_CLK_ENABLE();

        UART3_TX_PORT_CLK_ENABLE();
        UART3_RX_PORT_CLK_ENABLE();

        GPIO_InitStruct.Alternate = GPIO_AF7_USART3;
        GPIO_InitStruct.Pin       = UART3_TX_PIN;
        HAL_GPIO_Init(UART3_TX_PORT, &GPIO_InitStruct);
        
        GPIO_InitStruct.Alternate = GPIO_AF7_USART3;
        GPIO_InitStruct.Pin       = UART3_RX_PIN;
        HAL_GPIO_Init(UART3_RX_PORT, &GPIO_InitStruct);

        /* USART3 DMA Init */
        /* USART3_TX Init */
        __HAL_RCC_DMA1_CLK_ENABLE();
        hdma_u3tx.Instance                 = DMA1_Stream3;
        hdma_u3tx.Init.Channel             = DMA_CHANNEL_4;
        hdma_u3tx.Init.Direction           = DMA_MEMORY_TO_PERIPH;
        hdma_u3tx.Init.PeriphInc           = DMA_PINC_DISABLE;
        hdma_u3tx.Init.MemInc              = DMA_MINC_ENABLE;
        hdma_u3tx.Init.PeriphDataAlignment = DMA_PDATAALIGN_BYTE;
        hdma_u3tx.Init.MemDataAlignment    = DMA_MDATAALIGN_BYTE;
        hdma_u3tx.Init.Mode                = DMA_NORMAL;
        hdma_u3tx.Init.Priority            = DMA_PRIORITY_HIGH;
        hdma_u3tx.Init.FIFOMode            = DMA_FIFOMODE_DISABLE;
        if (HAL_DMA_Init(&hdma_u3tx) != HAL_OK)
        {
            _Error_Handler(__FILE__, __LINE__);
        }
        SET_BIT(huart->Instance->CR3, USART_CR3_DMAT);
        //SET_BIT(hdam_u3tx.Instance->CR, DMA_SxCR_DBM);
        /* 目标传输数量暂时不设定 */
        hdma_u3tx.Instance->PAR = (uint32_t)&USART3->DR;
        //hdma_u3tx.Instance->M0AR = r_DPM8600;
        //hdma_u3tx.Instance->M1AR = w_DPM8600;

        __HAL_LINKDMA(huart ,hdmatx, hdma_u3tx);

        /* USART3_RX Init */
        /*__HAL_RCC_DMA1_CLK_ENABLE();
        hdma_u3rx.Instance                  = DMA1_Stream1;
        hdma_u3rx.Init.Channel              = DMA_CHANNEL_4;
        hdma_u3rx.Init.Direction            = DMA_PERIPH_TO_MEMORY;
        hdma_u3rx.Init.PeriphInc            = DMA_PINC_DISABLE;
        hdma_u3rx.Init.MemInc               = DMA_MINC_ENABLE;
        hdma_u3rx.Init.PeriphDataAlignment  = DMA_PDATAALIGN_BYTE;
        hdma_u3rx.Init.MemDataAlignment     = DMA_MDATAALIGN_BYTE;
        hdma_u3rx.Init.Mode                 = DMA_CIRCULAR;
        hdma_u3rx.Init.Priority             = DMA_PRIORITY_HIGH;
        hdma_u3rx.Init.FIFOMode             = DMA_FIFOMODE_DISABLE;
        hdma_u3rx.Init.FIFOThreshold        = DMA_FIFO_THRESHOLD_FULL;
        hdma_u3rx.Init.MemBurst             = DMA_MBURST_INC4;
        hdma_u3rx.Init.PeriphBurst          = DMA_PBURST_INC4;
        if (HAL_DMA_Init(&hdma_u3rx) != HAL_OK)
        {
            _Error_Handler(__FILE__, __LINE__);
        }
        SET_BIT(huart->Instance->CR3, USART_CR3_DMAR);
        SET_BIT(hdma_u3rx.Instance->CR, DMA_SxCR_DBM);
        hdma_u3rx.Instance->NDTR  = (uint16_t)POWER_DATA_LENGTH;    //传输数据数
        hdma_u3rx.Instance->PAR	= (uint32_t)&USART3->DR;            //外设地址为串口DR寄存器地址
        hdma_u3rx.Instance->M0AR  = (uint32_t)uart3_rx_data[0];     //缓冲区0地址
		hdma_u3rx.Instance->M1AR  = (uint32_t)uart3_rx_data[1];
		
        __HAL_LINKDMA(huart, hdmarx, hdma_u3rx);*/

        HAL_NVIC_SetPriority(USART3_IRQn, 0, 0);
        HAL_NVIC_EnableIRQ(USART3_IRQn);
    }
    else if(huart->Instance == UART4)
    {
        __HAL_RCC_UART4_CLK_ENABLE();

        UART4_TX_PORT_CLK_ENABLE();
        UART4_RX_PORT_CLK_ENABLE();

        GPIO_InitStruct.Alternate = GPIO_AF8_UART4;
        GPIO_InitStruct.Pin       = UART4_TX_PIN;
        HAL_GPIO_Init(UART4_TX_PORT, &GPIO_InitStruct);
        
        GPIO_InitStruct.Alternate = GPIO_AF8_UART4;
        GPIO_InitStruct.Pin       = UART4_RX_PIN;
        HAL_GPIO_Init(UART4_RX_PORT, &GPIO_InitStruct);

        __HAL_RCC_DMA1_CLK_ENABLE();

        hdma_u4rx.Instance                  = DMA1_Stream2;
        hdma_u4rx.Init.Channel              = DMA_CHANNEL_4;
        hdma_u4rx.Init.Direction            = DMA_PERIPH_TO_MEMORY;
        hdma_u4rx.Init.PeriphInc            = DMA_PINC_DISABLE;
        hdma_u4rx.Init.MemInc               = DMA_MINC_ENABLE;
        hdma_u4rx.Init.PeriphDataAlignment  = DMA_PDATAALIGN_BYTE;
        hdma_u4rx.Init.MemDataAlignment     = DMA_MDATAALIGN_BYTE;
        hdma_u4rx.Init.Mode                 = DMA_CIRCULAR;
        hdma_u4rx.Init.Priority             = DMA_PRIORITY_VERY_HIGH;
        hdma_u4rx.Init.FIFOMode             = DMA_FIFOMODE_DISABLE;
        hdma_u4rx.Init.FIFOThreshold        = DMA_FIFO_THRESHOLD_FULL;
        hdma_u4rx.Init.MemBurst             = DMA_MBURST_INC4;
        hdma_u4rx.Init.PeriphBurst          = DMA_PBURST_INC4;
        if (HAL_DMA_Init(&hdma_u4rx) != HAL_OK)
        {
            _Error_Handler(__FILE__, __LINE__);
        }
        SET_BIT(UART4Handle.Instance->CR3, USART_CR3_DMAR);         //使能串口DMA接收模式
        SET_BIT(hdma_u4rx.Instance->CR, DMA_SxCR_DBM);              //使能DMA双缓冲模式，需要放在HAL_DMA_Init()之后
        hdma_u4rx.Instance->NDTR  = (uint16_t)RC_FRAME_LENGTH;      //传输数据数量
        hdma_u4rx.Instance->PAR	= (uint32_t)&UART4->DR;             //外设地址为串口DR寄存器地址
        hdma_u4rx.Instance->M0AR  = (uint32_t)uart4_rx_data[0];     //缓冲区0地址
        hdma_u4rx.Instance->M1AR  = (uint32_t)uart4_rx_data[1];     //缓冲区1地址

        __HAL_LINKDMA(huart, hdmarx, hdma_u4rx);

        HAL_NVIC_SetPriority(UART4_IRQn, 0, 0);
        HAL_NVIC_EnableIRQ(UART4_IRQn);       
    }  
    else if(huart->Instance == UART5)
    {
          __HAL_RCC_UART5_CLK_ENABLE();

        UART5_TX_PORT_CLK_ENABLE();
        UART5_RX_PORT_CLK_ENABLE();

        GPIO_InitStruct.Alternate = GPIO_AF8_UART5;
        GPIO_InitStruct.Pin       = UART5_TX_PIN;
        HAL_GPIO_Init(UART5_TX_PORT, &GPIO_InitStruct);
        GPIO_InitStruct.Alternate = GPIO_AF8_UART5;
        GPIO_InitStruct.Pin       = UART5_RX_PIN;
        HAL_GPIO_Init(UART5_RX_PORT, &GPIO_InitStruct);

        __HAL_RCC_DMA1_CLK_ENABLE();
        
        hdma_u5rx.Instance                  = DMA1_Stream0;
        hdma_u5rx.Init.Channel              = DMA_CHANNEL_4;
        hdma_u5rx.Init.Direction            = DMA_PERIPH_TO_MEMORY;
        hdma_u5rx.Init.PeriphInc            = DMA_PINC_DISABLE;
        hdma_u5rx.Init.MemInc               = DMA_MINC_ENABLE;
        hdma_u5rx.Init.PeriphDataAlignment  = DMA_PDATAALIGN_BYTE;
        hdma_u5rx.Init.MemDataAlignment     = DMA_MDATAALIGN_BYTE;
        hdma_u5rx.Init.Mode                 = DMA_CIRCULAR;
        hdma_u5rx.Init.Priority             = DMA_PRIORITY_VERY_HIGH;
        hdma_u5rx.Init.FIFOMode             = DMA_FIFOMODE_DISABLE;
        hdma_u5rx.Init.FIFOThreshold        = DMA_FIFO_THRESHOLD_FULL;
        hdma_u5rx.Init.MemBurst             = DMA_MBURST_INC4;
        hdma_u5rx.Init.PeriphBurst          = DMA_PBURST_INC4;

        if(HAL_DMA_Init(&hdma_u5rx) != HAL_OK)
        {
            _Error_Handler(__FILE__, __LINE__);
        }
        SET_BIT(UART5Handle.Instance->CR3, USART_CR3_DMAR);             //使能串口DMA接收模式
        SET_BIT(hdma_u5rx.Instance->CR, DMA_SxCR_DBM);                  //使能DMA双缓冲模式，需要放在HAL_DMA_Init()之后
        hdma_u5rx.Instance->NDTR    = (uint16_t)GYRO_DATA_MAX;          //传输数据数量
        hdma_u5rx.Instance->PAR	    = (uint32_t)&UART5->DR;             //外设地址为串口DR寄存器地址
        hdma_u5rx.Instance->M0AR    = (uint32_t)uart5_rx_data[0];       //缓冲区0地址
        hdma_u5rx.Instance->M1AR    = (uint32_t)uart5_rx_data[1];       //缓冲区1地址

        __HAL_LINKDMA(huart, hdmarx, hdma_u5rx);

        HAL_NVIC_SetPriority(UART5_IRQn, 0, 0);
        HAL_NVIC_EnableIRQ(UART5_IRQn);          
    }
    else if(huart->Instance == USART6)
    {
        __HAL_RCC_USART6_CLK_ENABLE();

        UART6_TX_PORT_CLK_ENABLE();
        UART6_RX_PORT_CLK_ENABLE();

        GPIO_InitStruct.Alternate = GPIO_AF8_USART6;
        GPIO_InitStruct.Pin       = UART6_TX_PIN;
        HAL_GPIO_Init(UART6_TX_PORT, &GPIO_InitStruct);
        
        GPIO_InitStruct.Alternate = GPIO_AF8_USART6;
        GPIO_InitStruct.Pin       = UART6_RX_PIN;
        HAL_GPIO_Init(UART6_RX_PORT, &GPIO_InitStruct);

        HAL_NVIC_SetPriority(USART6_IRQn, 1, 0);
        HAL_NVIC_EnableIRQ(USART6_IRQn);       
    }      
}

void HAL_UART_MspDeInit(UART_HandleTypeDef *huart)
{
    if(huart->Instance == USART1)
    {
        __HAL_RCC_USART1_FORCE_RESET();
        __HAL_RCC_USART1_RELEASE_RESET();

        __HAL_RCC_USART1_CLK_DISABLE();

        HAL_GPIO_DeInit(UART1_TX_PORT, UART1_TX_PIN);
        HAL_GPIO_DeInit(UART1_RX_PORT, UART1_RX_PIN);
    }
    else if(huart->Instance == USART2)
    {
        __HAL_RCC_USART2_FORCE_RESET();
        __HAL_RCC_USART2_RELEASE_RESET();

        __HAL_RCC_USART2_CLK_DISABLE();

        HAL_GPIO_DeInit(UART2_TX_PORT, UART2_TX_PIN);
        HAL_GPIO_DeInit(UART2_RX_PORT, UART2_RX_PIN);
    }
    else if(huart->Instance == USART3)
    {
        __HAL_RCC_USART3_FORCE_RESET();
        __HAL_RCC_USART3_RELEASE_RESET();

        __HAL_RCC_USART3_CLK_DISABLE();

        HAL_GPIO_DeInit(UART3_TX_PORT, UART3_TX_PIN);
        HAL_GPIO_DeInit(UART3_RX_PORT, UART3_RX_PIN);
    }    
    else if(huart->Instance == UART4)
    {
        __HAL_RCC_UART4_FORCE_RESET();
        __HAL_RCC_UART4_RELEASE_RESET();

        __HAL_RCC_UART4_CLK_DISABLE();

        HAL_GPIO_DeInit(UART4_TX_PORT, UART4_TX_PIN);
        HAL_GPIO_DeInit(UART4_RX_PORT, UART4_RX_PIN);
    }
    else if(huart->Instance == UART5)
    {
        __HAL_RCC_UART5_FORCE_RESET();
        __HAL_RCC_UART5_RELEASE_RESET();

        __HAL_RCC_UART5_CLK_DISABLE();

        HAL_GPIO_DeInit(UART5_TX_PORT, UART5_TX_PIN);
        HAL_GPIO_DeInit(UART5_RX_PORT, UART5_RX_PIN);
    } 
    else if(huart->Instance == USART6)
    {
        __HAL_RCC_USART6_FORCE_RESET();
        __HAL_RCC_USART6_RELEASE_RESET();

        __HAL_RCC_USART6_CLK_DISABLE();

        HAL_GPIO_DeInit(UART6_TX_PORT, UART6_TX_PIN);
        HAL_GPIO_DeInit(UART6_RX_PORT, UART6_RX_PIN);
    }
}



#ifdef __GNUC__
/* With GCC, small printf (option LD Linker->Libraries->Small printf
   set to 'Yes') calls __io_putchar() */
#define PUTCHAR_PROTOTYPE int __io_putchar(int ch)
#else
#define PUTCHAR_PROTOTYPE int fputc(int ch, FILE *f)
#endif /* __GNUC__ */

/**
  * @brief  Retargets the C library printf function to the USART.
  * @param  None
  * @retval None
  */
PUTCHAR_PROTOTYPE
{
    /* Place your implementation of fputc here */
    /* e.g. write a character to the USART1 and Loop until the end of transmission */
	while((USART1->SR&0X40)==0);//循环发送,直到发送完毕   
    USART1->DR = (uint8_t) ch;      
    return ch;
}




#elif HardwareType == Engineer_Hardware_Type

DMA_HandleTypeDef hdma_u3tx;

UART_HandleTypeDef UART1Handle;
UART_HandleTypeDef UART2Handle;
UART_HandleTypeDef UART3Handle;
UART_HandleTypeDef UART4Handle;
UART_HandleTypeDef UART5Handle;
UART_HandleTypeDef UART6Handle;

uint8_t uart1_rx_data[2][RC_FRAME_LENGTH];
uint8_t uart2_rx_data = 0;
uint8_t uart3_rx_data = 0;
uint8_t uart4_rx_data[32] = {0};
uint8_t uart5_rx_data[2][55u];
uint8_t uart6_rx_data[2][GYRO_DATA_MAX];
uint8_t length = 0;
__weak void BSP_UART1_RX_Callback(uint8_t rxdata[])
{
    return;
}

__weak void BSP_UART2_RX_Callback(uint8_t rxdata)
{
    return;
}

__weak void BSP_UART3_RX_Callback(uint8_t rxdata)
{
    return;
}

__weak void BSP_UART4_RX_Callback(uint8_t rxdata[])
{
    return;
}

__weak void BSP_UART5_RX_Callback(uint8_t rxdata[])
{
    return;
}

__weak void BSP_UART6_RX_Callback(uint8_t rxdata[])
{
    return;
}

__weak void MY_UART_IRQHandler(UART_HandleTypeDef* huart)
{
	if(huart->Instance == USART6)
    {
        uint8_t uart6RevDataLength = 0;
        if(__HAL_UART_GET_IT_SOURCE(&UART6Handle, UART_IT_IDLE) != RESET)
        {
            __HAL_UART_CLEAR_IDLEFLAG(&UART6Handle);
            uart6RevDataLength = GYRO_DATA_MAX - __HAL_DMA_GET_COUNTER(UART6Handle.hdmarx);
            length = uart6RevDataLength;
            if(UART6Handle.hdmarx->Instance->CR & DMA_SxCR_CT)
            {
                if(((uart6RevDataLength > 0) && (uart6RevDataLength < GYRO_ANGLE_DATA_LEN))		//??????,??????
				||((uart6RevDataLength > GYRO_ANGLE_DATA_LEN) && (uart6RevDataLength < GYRO_ANGV_DATA_LEN))
                ||((uart6RevDataLength > GYRO_ANGV_DATA_LEN) && (uart6RevDataLength < GYRO_DATA_MAX))
                )
                {
                    __HAL_DMA_DISABLE(UART6Handle.hdmarx);
                    UART6Handle.hdmarx->Instance->NDTR = (uint16_t)GYRO_DATA_MAX;
                    UART6Handle.hdmarx->Instance->CR  |= DMA_SxCR_CT;
                    __HAL_DMA_ENABLE(UART6Handle.hdmarx);
                }
                else
                {
					__HAL_DMA_DISABLE(UART6Handle.hdmarx);			
                   if(uart6RevDataLength == GYRO_ANGLE_DATA_LEN)
                   {
                        UART6Handle.hdmarx->Instance->NDTR = (uint16_t)GYRO_DATA_MAX;
                        UART6Handle.hdmarx->Instance->CR  |= DMA_SxCR_CT;
                   }
                   else if(uart6RevDataLength == GYRO_ANGV_DATA_LEN)
                   {
                     UART6Handle.hdmarx->Instance->NDTR = (uint16_t)GYRO_DATA_MAX;
                     UART6Handle.hdmarx->Instance->CR  |= DMA_SxCR_CT;
                   }
                    
                    BSP_UART6_RX_Callback(uart6_rx_data[1]);
					__HAL_DMA_ENABLE(UART6Handle.hdmarx);
                }
            }
            else
            {
                if(((uart6RevDataLength > 0) && (uart6RevDataLength < GYRO_ANGLE_DATA_LEN))
                ||((uart6RevDataLength > GYRO_ANGLE_DATA_LEN) && (uart6RevDataLength < GYRO_ANGV_DATA_LEN))
                ||((uart6RevDataLength > GYRO_ANGV_DATA_LEN) && (uart6RevDataLength < GYRO_DATA_MAX))
                )
                {
                    __HAL_DMA_DISABLE(UART6Handle.hdmarx);
                    UART6Handle.hdmarx->Instance->NDTR = (uint16_t)GYRO_DATA_MAX;
                    UART6Handle.hdmarx->Instance->CR  &= ~DMA_SxCR_CT;
                    __HAL_DMA_ENABLE(UART6Handle.hdmarx);
                }
                else
                {
					__HAL_DMA_DISABLE(UART6Handle.hdmarx);
                   if(uart6RevDataLength == GYRO_ANGLE_DATA_LEN)
                   {
                       UART6Handle.hdmarx->Instance->NDTR = (uint16_t)GYRO_DATA_MAX;
                       UART6Handle.hdmarx->Instance->CR  &= ~DMA_SxCR_CT;
                   }
                   else if(uart6RevDataLength == GYRO_ANGV_DATA_LEN)
                   {
                     UART6Handle.hdmarx->Instance->NDTR = (uint16_t)GYRO_DATA_MAX;
                     UART6Handle.hdmarx->Instance->CR  &= ~DMA_SxCR_CT;
                   }

                    BSP_UART6_RX_Callback(uart6_rx_data[0]);
					__HAL_DMA_ENABLE(UART6Handle.hdmarx);
                }
            }
        }
    }
	else if(huart->Instance == USART1)
    {
        uint8_t uart1RevDataLength = 0;           
        static uint16_t FailNumCount0 = 0, FailNumCount1 = 0;
        if(__HAL_USART_GET_IT_SOURCE(&UART1Handle, USART_IT_IDLE) != RESET)
        {
            __HAL_USART_CLEAR_IDLEFLAG(&UART1Handle);
            uart1RevDataLength = RC_FRAME_LENGTH - __HAL_DMA_GET_COUNTER(UART1Handle.hdmarx);
            
			if(UART1Handle.hdmarx->Instance->CR & DMA_SxCR_CT)   
            {
                if(uart1RevDataLength > 0 && uart1RevDataLength < RC_FRAME_LENGTH)        
                {
                    __HAL_DMA_DISABLE(UART1Handle.hdmarx);
                    UART1Handle.hdmarx->Instance->NDTR = (uint16_t)RC_FRAME_LENGTH;
                    UART1Handle.hdmarx->Instance->CR |= DMA_SxCR_CT;            
                    __HAL_DMA_ENABLE(UART1Handle.hdmarx);
                    FailNumCount1++;
                }
                else
                {
                    BSP_UART1_RX_Callback(uart1_rx_data[1]);
                }
            }
            else
            {
                if(uart1RevDataLength > 0 && uart1RevDataLength < RC_FRAME_LENGTH)
                {
                    __HAL_DMA_DISABLE(UART1Handle.hdmarx);
                    UART1Handle.hdmarx->Instance->NDTR = (uint16_t)RC_FRAME_LENGTH;
                    UART1Handle.hdmarx->Instance->CR &= ~DMA_SxCR_CT;
                    __HAL_DMA_ENABLE(UART1Handle.hdmarx);
                    FailNumCount0++;
                }
                else
                {
                    BSP_UART1_RX_Callback(uart1_rx_data[0]);
                }
            }
        }
    }
}

void HAL_UART_ErrorCallback(UART_HandleTypeDef *huart)
{
	if(huart->Instance == UART3Handle.Instance)
	{
		__HAL_UART_CLEAR_OREFLAG(&UART3Handle);
		HAL_UART_Receive_IT(&UART3Handle,&uart3_rx_data,1);
	}
	else if(huart->Instance == UART5Handle.Instance)
	{
		__HAL_UART_CLEAR_OREFLAG(&UART5Handle);
		HAL_UART_Receive_IT(&UART5Handle,&uart5_rx_data,1);
	}
	else if(huart->Instance == UART4Handle.Instance)
	{
		__HAL_UART_CLEAR_OREFLAG(&UART4Handle);
		HAL_UART_Receive_IT(&UART4Handle,uart4_rx_data,18);
	}
	else if(huart->Instance == UART2Handle.Instance)
	{
		__HAL_UART_CLEAR_OREFLAG(&UART2Handle);
		HAL_UART_Receive_IT(&UART2Handle,&uart2_rx_data,1);
	}
	else if(huart->Instance == UART1Handle.Instance)
	{
		__HAL_UART_CLEAR_OREFLAG(&UART1Handle);
		//HAL_UART_Receive_IT(&UART1Handle,uart1_rx_data,18);
	}
		else if(huart->Instance == UART6Handle.Instance)
	{
		__HAL_UART_CLEAR_OREFLAG(&UART6Handle);
		//HAL_UART_Receive_IT(&UART6Handle,&uart6_rx_data,1);
	}
}

void UART1_Init(uint32_t boundrate)
{

    UART1Handle.Instance            = USART1;
    UART1Handle.Init.BaudRate       = boundrate;
    UART1Handle.Init.WordLength     = UART_WORDLENGTH_8B;
    UART1Handle.Init.StopBits       = UART_STOPBITS_1;
    UART1Handle.Init.Parity         = UART_PARITY_NONE;
    UART1Handle.Init.Mode           = UART_MODE_TX_RX;
    UART1Handle.Init.HwFlowCtl      = UART_HWCONTROL_NONE;
    UART1Handle.Init.OverSampling   = UART_OVERSAMPLING_16;
    if (HAL_UART_Init(&UART1Handle) != HAL_OK)
    {
        _Error_Handler(__FILE__, __LINE__);
    }
    __HAL_DMA_ENABLE(UART1Handle.hdmarx);               //??DMA???
    __HAL_USART_ENABLE_IT(&UART1Handle, USART_IT_IDLE); 
//     if(HAL_UART_Receive_IT(&UART1Handle, uart1_rx_data, 18) != HAL_OK)
//     {
//         _Error_Handler(__FILE__, __LINE__);
//     }    
}

void UART2_Init(uint32_t boundrate)
{

    UART2Handle.Instance            = USART2;
    UART2Handle.Init.BaudRate       = boundrate;
    UART2Handle.Init.WordLength     = UART_WORDLENGTH_8B;
    UART2Handle.Init.StopBits       = UART_STOPBITS_1;
    UART2Handle.Init.Parity         = UART_PARITY_NONE;
    UART2Handle.Init.Mode           = UART_MODE_TX_RX;
    UART2Handle.Init.HwFlowCtl      = UART_HWCONTROL_NONE;
    UART2Handle.Init.OverSampling   = UART_OVERSAMPLING_16;
    if (HAL_UART_Init(&UART2Handle) != HAL_OK)
    {
        _Error_Handler(__FILE__, __LINE__);
    }

//    if(HAL_UART_Receive_IT(&UART2Handle, &uart2_rx_data, 1) != HAL_OK)
//    {
//        _Error_Handler(__FILE__, __LINE__);
//    }    
}

void UART3_Init(uint32_t boundrate)
{

    UART3Handle.Instance            = USART3;
    UART3Handle.Init.BaudRate       = boundrate;
    UART3Handle.Init.WordLength     = UART_WORDLENGTH_8B;
    UART3Handle.Init.StopBits       = UART_STOPBITS_1;
    UART3Handle.Init.Parity         = UART_PARITY_NONE;
    UART3Handle.Init.Mode           = UART_MODE_TX_RX;
    UART3Handle.Init.HwFlowCtl      = UART_HWCONTROL_NONE;
    UART3Handle.Init.OverSampling   = UART_OVERSAMPLING_16;
    if (HAL_UART_Init(&UART3Handle) != HAL_OK)
    {
        _Error_Handler(__FILE__, __LINE__);
    }

    if(HAL_UART_Receive_IT(&UART3Handle, &uart3_rx_data, 1) != HAL_OK)
    {
        _Error_Handler(__FILE__, __LINE__);
    }    
}

void UART4_Init(uint32_t boundrate)
{

    UART4Handle.Instance            = UART4;
    UART4Handle.Init.BaudRate       = boundrate;
    UART4Handle.Init.WordLength     = UART_WORDLENGTH_8B;
    UART4Handle.Init.StopBits       = UART_STOPBITS_1;
    UART4Handle.Init.Parity         = UART_PARITY_NONE;
    UART4Handle.Init.Mode           = UART_MODE_TX_RX;
    UART4Handle.Init.HwFlowCtl      = UART_HWCONTROL_NONE;
    UART4Handle.Init.OverSampling   = UART_OVERSAMPLING_16;
    if (HAL_UART_Init(&UART4Handle) != HAL_OK)
    {
        _Error_Handler(__FILE__, __LINE__);
    }

    if(HAL_UART_Receive_IT(&UART4Handle, uart4_rx_data, 18) != HAL_OK)
    {
        _Error_Handler(__FILE__, __LINE__);
    }    
}

void UART5_Init(uint32_t boundrate)
{

    UART5Handle.Instance            = UART5;
    UART5Handle.Init.BaudRate       = boundrate; 
    UART5Handle.Init.WordLength     = UART_WORDLENGTH_8B;
    UART5Handle.Init.StopBits       = UART_STOPBITS_1;
    UART5Handle.Init.Parity         = UART_PARITY_NONE;
    UART5Handle.Init.Mode           = UART_MODE_TX_RX;
    UART5Handle.Init.HwFlowCtl      = UART_HWCONTROL_NONE;
    UART5Handle.Init.OverSampling   = UART_OVERSAMPLING_16;
    if (HAL_UART_Init(&UART5Handle) != HAL_OK)
    {
        _Error_Handler(__FILE__, __LINE__);
    }  

    if(HAL_UART_Receive_IT(&UART5Handle, &uart5_rx_data, 1) != HAL_OK)
    {
        _Error_Handler(__FILE__, __LINE__);
    }    
}

void UART6_Init(uint32_t boundrate)
{

    UART6Handle.Instance            = USART6;
    UART6Handle.Init.BaudRate       = boundrate;
    UART6Handle.Init.WordLength     = UART_WORDLENGTH_8B;
    UART6Handle.Init.StopBits       = UART_STOPBITS_1;
    UART6Handle.Init.Parity         = UART_PARITY_NONE;
    UART6Handle.Init.Mode           = UART_MODE_TX_RX;
    UART6Handle.Init.HwFlowCtl      = UART_HWCONTROL_NONE;
    UART6Handle.Init.OverSampling   = UART_OVERSAMPLING_16;
    if (HAL_UART_Init(&UART6Handle) != HAL_OK)
    {
        _Error_Handler(__FILE__, __LINE__);
    }

//    if(HAL_UART_Receive_IT(&UART6Handle, &uart6_rx_data, 1) != HAL_OK)
//    {
//        _Error_Handler(__FILE__, __LINE__);
//    }    
		   __HAL_DMA_ENABLE(UART6Handle.hdmarx);				//??DMA???
       __HAL_UART_ENABLE_IT(&UART6Handle, UART_IT_IDLE);	//????????
}

void HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart)
{
    if(huart->Instance == USART1)
    {
//        BSP_UART1_RX_Callback(uart1_rx_data);

//        if(HAL_UART_Receive_IT(&UART1Handle, uart1_rx_data, 18) != HAL_OK)
//        {
//            _Error_Handler(__FILE__, __LINE__);
//        } 
    } 
    else if(huart->Instance == USART2)
    {
        BSP_UART2_RX_Callback(uart2_rx_data);

        if(HAL_UART_Receive_IT(&UART2Handle, &uart2_rx_data, 1) != HAL_OK)
        {
            _Error_Handler(__FILE__, __LINE__);
        } 
    }    
    else if(huart->Instance == USART3)
    {
        BSP_UART3_RX_Callback(uart3_rx_data);

        if(HAL_UART_Receive_IT(&UART3Handle, &uart3_rx_data, 1) != HAL_OK)
        {
            _Error_Handler(__FILE__, __LINE__);
        } 
    }        
    else if(huart->Instance == UART4)
    {
        BSP_UART4_RX_Callback(uart4_rx_data);

        if(HAL_UART_Receive_IT(&UART4Handle, uart4_rx_data, 18) != HAL_OK)
        {
            _Error_Handler(__FILE__, __LINE__);
        } 
    }    
    else if(huart->Instance == UART5)
    {
        BSP_UART5_RX_Callback(uart5_rx_data[]);

        if(HAL_UART_Receive_IT(&UART5Handle, &uart5_rx_data, 1) != HAL_OK)
        {
            _Error_Handler(__FILE__, __LINE__);
        } 
    }  
    else if(huart->Instance == USART6)
    {
//        BSP_UART6_RX_Callback(uart6_rx_data);

//        if(HAL_UART_Receive_IT(&UART6Handle, &uart6_rx_data, 1) != HAL_OK)
//        {
//            _Error_Handler(__FILE__, __LINE__);
//        } 
    }         
}

void HAL_UART_MspInit(UART_HandleTypeDef *huart)
{
    GPIO_InitTypeDef  GPIO_InitStruct = {0};

    static DMA_HandleTypeDef hdma_u1tx;
    static DMA_HandleTypeDef hdma_u1rx;
    static DMA_HandleTypeDef hdma_u5tx;		
    static DMA_HandleTypeDef hdma_u6rx;
		
    GPIO_InitStruct.Mode      = GPIO_MODE_AF_PP;
    GPIO_InitStruct.Pull      = GPIO_PULLUP;
    GPIO_InitStruct.Speed     = GPIO_SPEED_FREQ_VERY_HIGH;

    if(huart->Instance == USART1)
    {
        __HAL_RCC_USART1_CLK_ENABLE();

        UART1_TX_PORT_CLK_ENABLE();
        UART1_RX_PORT_CLK_ENABLE();

        GPIO_InitStruct.Alternate = GPIO_AF7_USART1;
        GPIO_InitStruct.Pin       = UART1_TX_PIN;
        HAL_GPIO_Init(UART1_TX_PORT, &GPIO_InitStruct);
        
        GPIO_InitStruct.Alternate = GPIO_AF7_USART1;
        GPIO_InitStruct.Pin       = UART1_RX_PIN;
        HAL_GPIO_Init(UART1_RX_PORT, &GPIO_InitStruct);

          __HAL_RCC_DMA2_CLK_ENABLE();

        hdma_u1rx.Instance                 = DMA2_Stream5;
        hdma_u1rx.Init.Channel             = DMA_CHANNEL_4;
        hdma_u1rx.Init.Direction           = DMA_PERIPH_TO_MEMORY;
        hdma_u1rx.Init.PeriphInc           = DMA_PINC_DISABLE;
        hdma_u1rx.Init.MemInc              = DMA_MINC_ENABLE;
        hdma_u1rx.Init.PeriphDataAlignment = DMA_PDATAALIGN_BYTE;
        hdma_u1rx.Init.MemDataAlignment    = DMA_MDATAALIGN_BYTE;
        hdma_u1rx.Init.Mode                = DMA_NORMAL;
        hdma_u1rx.Init.Priority            = DMA_PRIORITY_HIGH;
        hdma_u1rx.Init.FIFOMode            = DMA_FIFOMODE_DISABLE;         
        hdma_u1rx.Init.FIFOThreshold       = DMA_FIFO_THRESHOLD_FULL;
        hdma_u1rx.Init.MemBurst            = DMA_MBURST_INC4;
        hdma_u1rx.Init.PeriphBurst         = DMA_PBURST_INC4; 
        if (HAL_DMA_Init(&hdma_u1rx) != HAL_OK)
        {
            _Error_Handler(__FILE__, __LINE__);
        }
        SET_BIT(UART1Handle.Instance->CR3, USART_CR3_DMAR);           
        SET_BIT(hdma_u1rx.Instance->CR, DMA_SxCR_DBM);                  
        hdma_u1rx.Instance->NDTR    = (uint16_t)RC_FRAME_LENGTH;        
        hdma_u1rx.Instance->PAR	    = (uint32_t)&USART1->DR;            
        hdma_u1rx.Instance->M0AR    = (uint32_t)uart1_rx_data[0];       
        hdma_u1rx.Instance->M1AR    = (uint32_t)uart1_rx_data[1];       

        __HAL_LINKDMA(huart, hdmarx, hdma_u1rx);

        HAL_NVIC_SetPriority(USART1_IRQn, 0, 0);
        HAL_NVIC_EnableIRQ(USART1_IRQn);                      
    }
    else if(huart->Instance == USART2)
    {
        __HAL_RCC_USART2_CLK_ENABLE();

        UART2_TX_PORT_CLK_ENABLE();
        UART2_RX_PORT_CLK_ENABLE();

        GPIO_InitStruct.Alternate = GPIO_AF7_USART2;
        GPIO_InitStruct.Pin       = UART2_TX_PIN;
        HAL_GPIO_Init(UART2_TX_PORT, &GPIO_InitStruct);
        
//        GPIO_InitStruct.Alternate = GPIO_AF7_USART2;//此端口继电器占用
//        GPIO_InitStruct.Pin       = UART2_RX_PIN;
//        HAL_GPIO_Init(UART2_RX_PORT, &GPIO_InitStruct);

        HAL_NVIC_SetPriority(USART2_IRQn, 0, 0);
        HAL_NVIC_EnableIRQ(USART2_IRQn);    

    }  
    else if(huart->Instance == USART3)
    {
        __HAL_RCC_USART3_CLK_ENABLE();

        UART3_TX_PORT_CLK_ENABLE();
        UART3_RX_PORT_CLK_ENABLE();

        GPIO_InitStruct.Alternate = GPIO_AF7_USART3;
        GPIO_InitStruct.Pin       = UART3_TX_PIN;
        HAL_GPIO_Init(UART3_TX_PORT, &GPIO_InitStruct);
        
        GPIO_InitStruct.Alternate = GPIO_AF7_USART3;
        GPIO_InitStruct.Pin       = UART3_RX_PIN;
        HAL_GPIO_Init(UART3_RX_PORT, &GPIO_InitStruct);

		
        /* DMA controller clock enable */
        __HAL_RCC_DMA1_CLK_ENABLE();

		hdma_u3tx.Instance                 = DMA1_Stream3;
		hdma_u3tx.Init.Channel             = DMA_CHANNEL_4;
		hdma_u3tx.Init.Direction           = DMA_MEMORY_TO_PERIPH;
		hdma_u3tx.Init.PeriphInc           = DMA_PINC_DISABLE;
		hdma_u3tx.Init.MemInc              = DMA_MINC_ENABLE;
		hdma_u3tx.Init.PeriphDataAlignment = DMA_PDATAALIGN_BYTE;
		hdma_u3tx.Init.MemDataAlignment    = DMA_MDATAALIGN_BYTE;
		hdma_u3tx.Init.Mode                = DMA_NORMAL;
		hdma_u3tx.Init.Priority            = DMA_PRIORITY_LOW;
		hdma_u3tx.Init.FIFOMode            = DMA_FIFOMODE_DISABLE;
		
		HAL_DMA_Init(&hdma_u3tx);
        SET_BIT(UART3Handle.Instance->CR3, USART_CR3_DMAT);
        hdma_u3tx.Instance->PAR = (uint32_t)&USART3->DR;
        hdma_u3tx.Instance->M0AR = (uint32_t)clientTxData;

		__HAL_LINKDMA(huart,hdmatx,hdma_u3tx);	
		
        HAL_NVIC_SetPriority(USART3_IRQn, 0, 0);
        HAL_NVIC_EnableIRQ(USART3_IRQn);            
    }       
    else if(huart->Instance == UART4)
    {
        __HAL_RCC_UART4_CLK_ENABLE();

        UART4_TX_PORT_CLK_ENABLE();
        UART4_RX_PORT_CLK_ENABLE();

        GPIO_InitStruct.Alternate = GPIO_AF8_UART4;
        GPIO_InitStruct.Pin       = UART4_TX_PIN;
        HAL_GPIO_Init(UART4_TX_PORT, &GPIO_InitStruct);
        
        GPIO_InitStruct.Alternate = GPIO_AF8_UART4;
        GPIO_InitStruct.Pin       = UART4_RX_PIN;
        HAL_GPIO_Init(UART4_RX_PORT, &GPIO_InitStruct);

        HAL_NVIC_SetPriority(UART4_IRQn, 0, 0);
        HAL_NVIC_EnableIRQ(UART4_IRQn);       
    }  
    else if(huart->Instance == UART5)
    {
        __HAL_RCC_UART5_CLK_ENABLE();

        UART5_TX_PORT_CLK_ENABLE();
        UART5_RX_PORT_CLK_ENABLE();

        GPIO_InitStruct.Alternate = GPIO_AF8_UART5;
        GPIO_InitStruct.Pin       = UART5_TX_PIN;
        HAL_GPIO_Init(UART5_TX_PORT, &GPIO_InitStruct);
        
        GPIO_InitStruct.Alternate = GPIO_AF8_UART5;
        GPIO_InitStruct.Pin       = UART5_RX_PIN;
        HAL_GPIO_Init(UART5_RX_PORT, &GPIO_InitStruct);

        __HAL_RCC_DMA1_CLK_ENABLE();
        hdma_u5tx.Instance                 = DMA1_Stream7;
        hdma_u5tx.Init.Channel             = DMA_CHANNEL_4;
        hdma_u5tx.Init.Direction           = DMA_MEMORY_TO_PERIPH;
        hdma_u5tx.Init.PeriphInc           = DMA_PINC_DISABLE;
        hdma_u5tx.Init.MemInc              = DMA_MINC_ENABLE;
        hdma_u5tx.Init.PeriphDataAlignment = DMA_PDATAALIGN_BYTE;
        hdma_u5tx.Init.MemDataAlignment    = DMA_MDATAALIGN_BYTE;
        hdma_u5tx.Init.Mode                = DMA_NORMAL;
        hdma_u5tx.Init.Priority            = DMA_PRIORITY_LOW;
        hdma_u5tx.Init.FIFOMode            = DMA_FIFOMODE_DISABLE;
        hdma_u5tx.Init.FIFOThreshold       = DMA_FIFO_THRESHOLD_FULL;
        hdma_u5tx.Init.MemBurst            = DMA_MBURST_INC4;
        hdma_u5tx.Init.PeriphBurst         = DMA_PBURST_INC4;
        HAL_DMA_Init(&hdma_u5tx);
        __HAL_LINKDMA(huart, hdmatx, hdma_u5tx);

        HAL_NVIC_SetPriority(UART5_IRQn, 0, 1);
        HAL_NVIC_EnableIRQ(UART5_IRQn);          
    }
    else if(huart->Instance == USART6)
    {
        __HAL_RCC_USART6_CLK_ENABLE();

        UART6_TX_PORT_CLK_ENABLE();
        UART6_RX_PORT_CLK_ENABLE();

        GPIO_InitStruct.Alternate = GPIO_AF8_USART6;
        GPIO_InitStruct.Pin       = UART6_TX_PIN;
        HAL_GPIO_Init(UART6_TX_PORT, &GPIO_InitStruct);
        
        GPIO_InitStruct.Alternate = GPIO_AF8_USART6;
        GPIO_InitStruct.Pin       = UART6_RX_PIN;
        HAL_GPIO_Init(UART6_RX_PORT, &GPIO_InitStruct);

			  __HAL_RCC_DMA2_CLK_ENABLE();
			  
			     
        hdma_u6rx.Instance                  = DMA2_Stream1;//���޸�
        hdma_u6rx.Init.Channel              = DMA_CHANNEL_5;
        hdma_u6rx.Init.Direction            = DMA_PERIPH_TO_MEMORY;
        hdma_u6rx.Init.PeriphInc            = DMA_PINC_DISABLE;
        hdma_u6rx.Init.MemInc               = DMA_MINC_ENABLE;
        hdma_u6rx.Init.PeriphDataAlignment  = DMA_PDATAALIGN_BYTE;
        hdma_u6rx.Init.MemDataAlignment     = DMA_MDATAALIGN_BYTE;
        hdma_u6rx.Init.Mode                 = DMA_CIRCULAR;
        hdma_u6rx.Init.Priority             = DMA_PRIORITY_VERY_HIGH;
        hdma_u6rx.Init.FIFOMode             = DMA_FIFOMODE_DISABLE;
        hdma_u6rx.Init.FIFOThreshold        = DMA_FIFO_THRESHOLD_FULL;
        hdma_u6rx.Init.MemBurst             = DMA_MBURST_INC4;
        hdma_u6rx.Init.PeriphBurst          = DMA_PBURST_INC4;

        if(HAL_DMA_Init(&hdma_u6rx) != HAL_OK)
        {
            _Error_Handler(__FILE__, __LINE__);
        }
        SET_BIT(UART6Handle.Instance->CR3, USART_CR3_DMAR);             
        SET_BIT(hdma_u6rx.Instance->CR, DMA_SxCR_DBM);                  
        hdma_u6rx.Instance->NDTR    = (uint16_t)GYRO_DATA_MAX;         
        hdma_u6rx.Instance->PAR	    = (uint32_t)&USART6->DR;             
        hdma_u6rx.Instance->M0AR    = (uint32_t)uart6_rx_data[0];      
        hdma_u6rx.Instance->M1AR    = (uint32_t)uart6_rx_data[1];       

        __HAL_LINKDMA(huart, hdmarx, hdma_u6rx);
				
        HAL_NVIC_SetPriority(USART6_IRQn, 0, 0);
        HAL_NVIC_EnableIRQ(USART6_IRQn);       
    }      
}

void HAL_UART_MspDeInit(UART_HandleTypeDef *huart)
{
    if(huart->Instance == USART1)
    {
        __HAL_RCC_USART1_FORCE_RESET();
        __HAL_RCC_USART1_RELEASE_RESET();

        __HAL_RCC_USART1_CLK_DISABLE();

        HAL_GPIO_DeInit(UART1_TX_PORT, UART1_TX_PIN);
        HAL_GPIO_DeInit(UART1_RX_PORT, UART1_RX_PIN);
    }
    else if(huart->Instance == USART2)
    {
        __HAL_RCC_USART2_FORCE_RESET();
        __HAL_RCC_USART2_RELEASE_RESET();

        __HAL_RCC_USART2_CLK_DISABLE();

        HAL_GPIO_DeInit(UART2_TX_PORT, UART2_TX_PIN);
        HAL_GPIO_DeInit(UART2_RX_PORT, UART2_RX_PIN);
    }
    else if(huart->Instance == USART3)
    {
        __HAL_RCC_USART3_FORCE_RESET();
        __HAL_RCC_USART3_RELEASE_RESET();

        __HAL_RCC_USART3_CLK_DISABLE();

        HAL_GPIO_DeInit(UART3_TX_PORT, UART3_TX_PIN);
        HAL_GPIO_DeInit(UART3_RX_PORT, UART3_RX_PIN);
    }    
    else if(huart->Instance == UART4)
    {
        __HAL_RCC_UART4_FORCE_RESET();
        __HAL_RCC_UART4_RELEASE_RESET();

        __HAL_RCC_UART4_CLK_DISABLE();

        HAL_GPIO_DeInit(UART4_TX_PORT, UART4_TX_PIN);
        HAL_GPIO_DeInit(UART4_RX_PORT, UART4_RX_PIN);
    }
    else if(huart->Instance == UART5)
    {
        __HAL_RCC_UART5_FORCE_RESET();
        __HAL_RCC_UART5_RELEASE_RESET();

        __HAL_RCC_UART5_CLK_DISABLE();

        HAL_GPIO_DeInit(UART5_TX_PORT, UART5_TX_PIN);
        HAL_GPIO_DeInit(UART5_RX_PORT, UART5_RX_PIN);
    } 
    else if(huart->Instance == USART6)
    {
        __HAL_RCC_USART6_FORCE_RESET();
        __HAL_RCC_USART6_RELEASE_RESET();

        __HAL_RCC_USART6_CLK_DISABLE();

        HAL_GPIO_DeInit(UART6_TX_PORT, UART6_TX_PIN);
        HAL_GPIO_DeInit(UART6_RX_PORT, UART6_RX_PIN);
    }
}

#endif
